package cn.gok.service.impl;

import cn.gok.dao.StuSecurityQuestionAnswerDAO;
import cn.gok.dao.StudentDAO;
import cn.gok.entity.SecurityQuestion;
import cn.gok.entity.StuSecurityQuestionAnswer;
import cn.gok.entity.Student;
import cn.gok.service.IStudentService;
import cn.gok.util.MybatisUtil;
import cn.gok.util.ResultData;
import cn.gok.util.SecurityQuestionCacheUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @Description:
 * @Author: chen fan
 * @Date: 2022年10月18日 15:28:21
 */

public class StudentServiceImpl implements IStudentService {
    private SqlSession sqlSession = MybatisUtil.getSQLSession();

    /**
     * 处理登录业务逻辑
     * @param studentName  登录用户名
     * @param password     密码
     * @return
     */
    public ResultData login(String studentName, String password){

        StudentDAO studentDAO = sqlSession.getMapper(StudentDAO.class);
        Student student = studentDAO.findStudentByNameAndPassword(studentName, password);

        if (student == null){
            return new ResultData(ResultData.LOGIN_FAIL,"登录失败",null);
        }

        student.setPassword(null);
        return new ResultData(ResultData.LOGIN_SUCCESS,"登录成功",student);
    }

    @Override
    public ResultData queryQuestion(String stuNameOrPhoneNo) {
        StudentDAO studentDAO = sqlSession.getMapper(StudentDAO.class);
        StuSecurityQuestionAnswerDAO answerDAO = sqlSession.getMapper(StuSecurityQuestionAnswerDAO.class);

        int stuId = studentDAO.findSutIdByNameOrPhoneNo(stuNameOrPhoneNo);
        List<String> questions = answerDAO.queryQuestionsByStudentName(stuNameOrPhoneNo);

        ResultData resultData = new ResultData();
        resultData.put("code",ResultData.OPERATION_SUCCESS);
        resultData.put("questions",questions);
        resultData.put("stuId",stuId);
        return resultData;
    }

    @Override
    public ResultData checkAnswer(String sessionId, int stuId, String question, String answer) {
        StuSecurityQuestionAnswerDAO answerDAO = sqlSession.getMapper(StuSecurityQuestionAnswerDAO.class);
        StuSecurityQuestionAnswer questionAnswer = answerDAO.queryStuSecurityQuestionAnswer(stuId, question, answer);

        //如果 密保问题验证失败，则直接返回提示结果
        if ( questionAnswer == null){
            return ResultData.fail(ResultData.CHECK_SECURITY_QUESTION_FAIL,"密保问题验证失败，请重试");
        }

        /*
            如果密保问题验证成功，则需要向缓存中存放当前用户的信息，
            以便于在后续的修改密码操作中，做验证（指确保用户是通过了密保验证，才去修改的密码）

                缓存中的 key ：    user_用户的sessionId
                       value值： stuId
         */
        SecurityQuestionCacheUtil.put(SecurityQuestionCacheUtil.prefix + sessionId , String.valueOf(stuId));
        return ResultData.ok(ResultData.CHECK_SECURITY_QUESTION_SUCCESS,"密保验证通过");
    }

    @Override
    public ResultData forgetUpdatePassword(String sessionId,String newPassword) {
        //先去缓存中查看，用户是否已经通过 密保验证
        String value = SecurityQuestionCacheUtil.getValue(SecurityQuestionCacheUtil.prefix + sessionId);
        if ( value == null){
            return ResultData.fail(ResultData.OPERATION_FAIL, "请求不合法，请通过验证后再进行密码修改");
        }

        Integer stuId = Integer.parseInt(value);

        StudentDAO studentDAO = sqlSession.getMapper(StudentDAO.class);
        int i = studentDAO.updatePasswordById(stuId, newPassword);
        if ( i > 0){
            return ResultData.ok(ResultData.OPERATION_SUCCESS,"重置密码成功");
        }
        return ResultData.fail(ResultData.OPERATION_FAIL,"重置密码失败，请稍后尝试");
    }
}
